/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

// NOTE: i^2 = j^2 = j^2 = ijk = -1

class	EQuat	{
	public:
		union {
			float	q[4];
			float	v[4];
			struct	{ float x,y,z,w; };
		};
		
					EQuat		( void );
					EQuat		( float w );
					EQuat		( float x, float y, float z, float w );
					EQuat		( const EQuat &Q );
					EQuat		( const EVec4 &v );

		EQuat		operator-	( void ) const;				
		EQuat		operator+	( const EQuat &B ) const;
		EQuat		operator-	( const EQuat &B ) const;
		EQuat		operator*	( const EQuat &B ) const;
		EQuat		operator/	( const EQuat &B ) const;
		
		EQuat		&operator+=	( const EQuat &B ) { *this = *this + B; return *this; }
		EQuat		&operator-=	( const EQuat &B ) { *this = *this - B; return *this; }
		EQuat		&operator*=	( const EQuat &B ) { *this = *this * B; return *this; }
		EQuat		&operator/=	( const EQuat &B ) { *this = *this / B; return *this; }
		
		bool		operator==	( const EQuat &B ) const;
		bool		operator!=	( const EQuat &B ) const;
		
		EQuat		Conjugate	( void ) const;
		float		Abs			( void ) const;
		const char	*ToStr		( void ) const;
		
		void		MakeRotation( const EVec4 &axis, float angle );		// make rotation quat, length of vector must be 1.0
		EVec4		RotateCW	( const	EVec4 &v ) const;				// rotate vector, quat must be normalized
		EVec4		RotateCCW	( const	EVec4 &v ) const;				// rotate vector, quat must be normalized
		
		EVec4		ToVec4		( void ) const { return EVec4(x,y,z,w); }
		EVec3		ToVec3		( void ) const { return EVec3(x,y,z); }
	};
	
	

inline EQuat::EQuat( void ) {
	x=y=z=w=0;
}

inline EQuat::EQuat( float w ) {
	x=y=z=0;
	this->w = w;
}

inline EQuat::EQuat( float x, float y, float z, float w ) {
	this->x = x;
	this->y = y;
	this->z = z;
	this->w = w;
}

inline EQuat::EQuat( const EQuat &Q ) {
	x = Q.x;
	y = Q.y;
	z = Q.z;
	w = Q.w;
}

inline EQuat::EQuat( const EVec4 &v ) {
	x = v.x;
	y = v.y;
	z = v.z;
	w = v.w;
}


inline EQuat EQuat::operator - ( void ) const {
	return EQuat( -x, -y, -z, -w );
}

inline EQuat EQuat::operator + ( const EQuat &B ) const {
	return EQuat( x+B.x, y+B.y, z+B.z, w+B.w );
}

inline EQuat EQuat::operator - ( const EQuat &B ) const {
	return EQuat( x-B.x, y-B.y, z-B.z, w-B.w );
}

inline EQuat EQuat::operator * ( const EQuat &B ) const 
{
	EVec3	im_A(x, y, z);			// p
	EVec3	im_B(B.x, B.y, B.z);	// q
	float	re_A = w;
	float	re_B = B.w;

	float	re_R = (re_A * re_B) - (im_A | im_B);
	EVec3	im_R = (re_A * im_B) + (re_B * im_A) + (im_A ^ im_B);
	
	return EQuat( im_R.x, im_R.y, im_R.z,   re_R);
}

inline EQuat EQuat::operator / ( const EQuat &B ) const 
{
	EQuat	Q = *this * B.Conjugate();
	float	d = ( B * B.Conjugate() ).w;
	
	Q.x /= d;
	Q.y /= d;
	Q.z /= d;
	Q.w /= d;
	
	return Q;
}

inline bool		EQuat::operator == ( const EQuat &B ) const {
	return ( x==B.x && y==B.y && z==B.z && w==B.w );
}

inline bool		EQuat::operator != ( const EQuat &B ) const {
	return !(*this == B);
}


inline EQuat EQuat::Conjugate(void) const {
	return EQuat( -x, -y, -z, w );
}

inline float EQuat::Abs(void) const {
	return sqrt( x*x + y*y + z*z + w*w );
}

inline const char *EQuat::ToStr(void) const {
	return va("[(%10.4f %10.4f %10.4f) %10.4f]", x,y,z,w);
}



inline void EQuat::MakeRotation(const EVec4 &axis, float angle) {
	float c = cosf(angle/2.0f);
	float s = sinf(angle/2.0f);
	
	x = axis.x * s;
	y = axis.y * s;
	z = axis.z * s;
	w = c;
}

inline EVec4 EQuat::RotateCW(const EVec4 &v ) const {
	const EQuat qt	= v;		
	const EQuat &qr	= *this;
	return 	EVec4((qr.Conjugate() * qt * qr).v);
}

inline EVec4 EQuat::RotateCCW(const EVec4 &v ) const {
	const EQuat qt	= v;		
	const EQuat &qr	= *this;
	return 	EVec4((qr * qt * qr.Conjugate()).v);
}










